# -*- coding: UTF-8 -*-
# @Time : 2022/1/15 0015 13:27
# @Author : lyw RSW
# @Remark : 机柜
import time

from mxsoftpy import Server
from mxsoftpy.exception import ParamsError

from db.common.CRM.server_cabinet import CabinetDB
from model.common.CRM.server_cabinet import CabinetUpdateModel, CabinetModel
from utils.public.params_method import get_pagination_params, pagination, paging_data
from index import _


class CabinetServer(Server):
    __model__ = CabinetModel
    __db__ = CabinetDB

    def list(self, **kwargs):
        """
        机柜详情页(点击组时显示)
        :param kwargs: 校验后的参数 dict
        :return:
        """

        """获取分页"""
        paging_dict = get_pagination_params(kwargs)
        paging_dict['page_size'] = paging_dict['page_size'] if paging_dict.get('page_size') else 20
        paging_dict['page_index'] = paging_dict['page_index'] if paging_dict.get('page_index') else 1
        search_dict = {}
        if kwargs.get('cabinet_search') and kwargs['cabinet_search']:
            search_dict = {'name__nclike': '*' + kwargs['cabinet_search'] + '*'}

        """进行查询"""
        data_len, data_list = self.db.list(kwargs['sroom_parent'],kwargs.get("state",None), **search_dict)
        """增加分页"""
        data_dict = paging_data(data_list, **paging_dict)
        return '操作完成', data_dict

    def add(self, **kwargs):
        """
        添加机柜
        :param kwargs:  校验后的参数 dict
        :return:
        """
        add_data_dict = self.model(**kwargs).dict()  # 校验后的数据
        return self.db.add(**add_data_dict)

    def retrieve(self, **kwargs):
        """
        回显机柜
        :param kwargs:  校验后的参数 dict
        :return:
        """
        return self.db.retrieve(kwargs['cabinet_code'])

    def select(self, **kwargs):
        """
        添加已有的机柜
        :param kwargs: 机柜集合 dict
        :return:
        """
        return self.db.select(kwargs['code_list'], kwargs['sroom_parent'])

    def update(self, **kwargs):
        """
        更新机柜
        :param kwargs:  校验后的参数 dict
        :return:
        """
        """校验修改字段"""
        update_data_dict = CabinetUpdateModel(**kwargs).dict(exclude_none=True)
        if update_data_dict or kwargs.get('total_space'):
            if kwargs.get('total_space'):
                try:
                    update_data_dict['total_space'] = int(kwargs['total_space'])
                except ValueError:
                    raise ParamsError('容量无法转为int类型')
            update_data_dict['update_time'] = int(time.time())
            return self.db.update(kwargs['cabinet_code'], **update_data_dict)
        return _('操作完成'), '没有要修改的属性'

    def unbound(self, **kwargs):
        """
        移除机柜
        :param kwargs: 机柜集合 dict
        :return:
        """

        return self.db.unbound(kwargs['code_list'])

    def delete(self, **kwargs):
        """
        删除机柜
        :param kwargs: 机柜集合 dict
        :return:
        """
        return self.db.delete(kwargs['code_list'])

    def Distribution(self, **kwargs):
        """
        分配机房
        :param kwargs:校验后的参数
        :return:
        """
        return self.db.distribution(kwargs['cabinet_code'], kwargs['sroom_parent'])
